import {TilesRenderer} from '3d-tiles-renderer';
import GeoUtils from '../../utils/GeoUtils';

export class CustomTilesRender extends TilesRenderer {
    constructor( options ) {
		super( options.url );
        this.options = options;
        this.isRootJson = true;
    }

    preprocessNode(tile, tileSetDir, parentTile = null){
        if(this.isRootJson == true && 
            (tile != undefined || tile != null) && 
            (tile.transform != undefined || tile.transform != null)){
                let transform = tile.transform;
                let height = 0; //transform[14];

                let boundingVolume = tile.boundingVolume;
                if (boundingVolume.box) {
                    height += boundingVolume.box[2];
                    height += boundingVolume.box[11];
                } else if (boundingVolume.sphere) {
                    height += boundingVolume.sphere[2];
                    height += boundingVolume.sphere[3]; 
                } else if (boundingVolume.region) {
                    height += boundingVolume.region[2];
                    height += (boundingVolume.region[5] - boundingVolume.region[4]);
                }

                if (this.options.geoSystem == '4978' || !this.options.geoSystem) { //ecef坐标
                    let rootPositionWGS =  GeoUtils.threedtilePointToWGS( transform[12], transform[13], transform[14] ); //从ecef坐标转换到地理坐标
                    if(this.options.isCloseGround){
                        rootPositionWGS.z = height; //把高度置为height
                    }
                    
                    if(typeof this.options.z === 'number'){
                        rootPositionWGS.z += this.options.z; //调整高度
                    }
                    let rootPositionEcef = GeoUtils.WGSTothreedtilePoint(rootPositionWGS); //从地理坐标转换到ecef坐标
                    transform[12] = rootPositionEcef.x;
                    transform[13] = rootPositionEcef.y;
                    transform[14] = rootPositionEcef.z;
                }else{ //平面坐标
                    if(typeof this.options.z === 'number'){
                        transform[14] += (this.options.z);
                    }
                }

               
        }
        this.isRootJson = false;

        super.preprocessNode(tile, tileSetDir, parentTile);
    }
}